import { ShoppingService } from './shopping';
import { StationService } from './station';
// import app from 'admin/application';
import { AccountService } from 'admin/services/account';
import * as wuzhui from "maishu-wuzhui";
import * as chitu from "maishu-chitu";
import { errorHandle } from 'admin/errorHandle';

let shopping = new ShoppingService();
shopping.error.add((sender, err) => errorHandle(err));

let station = new StationService();
station.error.add((sender, err) => errorHandle(err));

export let pageData = new wuzhui.DataSource<PageData>({
    primaryKeys: ['id'],
    select: (args) => {
        //===================================================
        // 服务端过滤有 BUG，这么写无效
        // Type is null or Type == 'page'
        args.filter = `(Type is null) or (Type == 'page')`;
        //===================================================
        return station.pageList(args);
    },
    insert: (item, args: { isSystem }) => {
        item.type = 'page';
        return station.savePageData(item);
    },
    update: (item) => {
        return station.savePageData(item)
    },
    delete: (item) => station.deletePageData(item.id)
})

export let productTemplate: wuzhui.DataSource<PageData> = Object.assign(new wuzhui.DataSource<PageData>({
    primaryKeys: ['id'],
    select: (args) => station.productTemplateList(args),
    insert: (item, args: { isSystem }) => station.savePageData(item),
    update: (item) => station.savePageData(item),
    delete: (item) => station.deletePageData(item.id)
}), { count: new chitu.ValueStore() })

export let product: wuzhui.DataSource<Product> = window['product_dataSource'] ||
    new wuzhui.DataSource<Product>({
        primaryKeys: ['Id'],
        select: (args) => shopping.products(args),
        delete: (item) => shopping.deleteProduct(item.Id),
        insert: (item, args) => {
            args = args || {};
            return shopping.saveProduct({
                product: item, parentId: args.parentId, id: args.id
            })
        },
        update: (item, args) => {
            args = args || {};
            return shopping.saveProduct({
                product: item, parentId: args.parentId, id: args.id
            })
        }
    })

let account = new AccountService();
account.error.add((sender, err) => errorHandle(err));
export let receivementAccount = new wuzhui.DataSource<ReceivementAccount>({
    primaryKeys: ['Id'],
    select: async () => {
        let dataItems = await account.receivementAccountList();
        return { dataItems, totalRowCount: dataItems.length };
    },
    insert: (item) => account.saveReceivementAccount(item),
    update: (item) => account.saveReceivementAccount(item)
});

export let freightSolution = new wuzhui.DataSource({
    select: async () => {
        let dataItems = await shopping.freightSolutions();
        return { dataItems, totalRowCount: dataItems.length };
    },
    delete: (dataItem) => shopping.deleteFreightSolution(dataItem),
    update: (dataItem) => shopping.updateFreightSolution(dataItem),
    insert: (dataItem) => shopping.addFreightSolution(dataItem),
    primaryKeys: ['Id']
});

export let coupon = new wuzhui.DataSource<Coupon>({
    primaryKeys: ['Id'],
    select: async () => {
        let dataItems = await shopping.coupons();
        return { dataItems, totalRowCount: dataItems.length };
    },
    delete: (dataItem) => shopping.deleteCoupon(dataItem),
    update: (dataItem) => shopping.saveCoupon(dataItem),
    insert: (dataItem) => shopping.saveCoupon(dataItem),
});

window['product_dataSource'] = window['product_dataSource'] || product;

